package com.peng.order.mqtt;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.peng.order.pojo.dto.ChargingDto;
import com.peng.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * @author： Peng
 * @date： 2024/6/27
 * @version: 1.0
 */
@Component
@Slf4j
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;

    private String topic="/topic/start/charging";

    public void send(ChargingDto message) throws JsonProcessingException {
        //将消息转换为JSON格式
        String json = JsonUtils.toJson(message);
        try {
            log.debug("MqttProducer send message: 入参--topic{}，message{}", topic, json);
            mqttClient.publish(topic, json.getBytes(), 0, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
